package com.lancoo.answer.view.xgPaResultView.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvXgAdapterAnswersheetChildBinding
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.util.DensityUtils
import com.lancoo.answer.view.base.XBaseRecyclerAdapter
import com.lancoo.answer.view.base.XBaseRecyclerHolder

class XgPaResultAnswerSheetChildAdapter(
    val context: Context,
    val cognitiveBean: CognitiveBean,
    var indexA: Int
) : XBaseRecyclerAdapter<EvXgAdapterAnswersheetChildBinding>() {
    var indexList: MutableList<String> = ArrayList()

    override fun onBindingView(viewGroup: ViewGroup?): EvXgAdapterAnswersheetChildBinding {
        return EvXgAdapterAnswersheetChildBinding.inflate(
            LayoutInflater.from(
                viewGroup!!.context
            ), viewGroup, false
        )
    }

    override fun onBindingData(
        holder: XBaseRecyclerHolder<EvXgAdapterAnswersheetChildBinding>,
        position: Int
    ) {

        val mTvSortIndex = holder.viewBinding.TvSortIndex

        var newSortIndex = cognitiveBean.TypeList?.get(indexA)!!.QuesList!![0]
            .SortIndexList!![0].NewSortIndex!!

        if (!newSortIndex.contains("-")) {
            newSortIndex = (newSortIndex.toInt() + position).toString()
        }
        indexList.add(newSortIndex)
        var indexB = positionReturnIndexB(cognitiveBean, position)
        var indexC = positionReturnIndexC(cognitiveBean, position)
        var scoreRate = cognitiveBean.TypeList?.get(indexA)!!.QuesList!![indexB]
            .SortIndexList!![indexC].ScoreRate
        var state = cognitiveBean.TypeList?.get(indexA)!!.QuesList!![indexB]
            .SortIndexList!![indexC].State
        mTvSortIndex.text = newSortIndex.toString()

        if (state == -1 || state == 0) {
            mTvSortIndex.setBackgroundResource(R.drawable.button_round_blue)
        } else {
            if (scoreRate!! >= 0.6) {
                mTvSortIndex.setBackgroundResource(R.drawable.button_round_green)
            } else {
                mTvSortIndex.setBackgroundResource(R.drawable.button_round_red)
            }
        }


        holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)

        holder.viewBinding.TvSortIndex.setOnClickListener {
            var index_C = positionReturnIndexC(cognitiveBean, position)
            clickChilidListener!!.onClick(indexA, indexB, index_C)
        }
    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = cognitiveBean.TypeList!![indexA].QuesList!!.size
        for (b in 0 until quesListSize) {
            itemCount += cognitiveBean.TypeList!![indexA].QuesList!![b].SortIndexList!!.size
        }
        return itemCount
    }

    interface OnClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun positionReturnIndexB(
        cognitiveBean: CognitiveBean,
        position: Int
    ): Int {

        var postionX = -1
        var index_B = 0
        var QuesListSize = cognitiveBean.TypeList?.get(indexA)?.QuesList!!.size
        outLoop@ for (b in 0 until QuesListSize) {
            val sortIndexList = cognitiveBean.TypeList!![indexA].QuesList!![b]
                .SortIndexList!!
            val sortIndexListSize = sortIndexList.size

            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (indexList[position] == sortIndexList[c].NewSortIndex) {
                    index_B = b
                    break@outLoop
                }
            }
        }
        return index_B
    }


    private fun positionReturnIndexC(
        cognitiveBean: CognitiveBean,
        position: Int
    ): Int {
        var postionX = -1
        var index_C = 0
        var QuesListSize = cognitiveBean.TypeList?.get(indexA)?.QuesList!!.size
        outLoop@ for (b in 0 until QuesListSize) {
            val sortIndexList = cognitiveBean.TypeList!![indexA].QuesList!![b]
                .SortIndexList!!
            val sortIndexListSize = sortIndexList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (indexList[position] == sortIndexList[c].NewSortIndex) {

                    index_C = c
                    break@outLoop
                }
            }
        }
        return index_C
    }

}